<?php
/**
 * 登录文件
 *
 * @author lwb
 */
namespace app\wechat\controller;

class Login extends Common {

    /**
     * 登录
     */
    public function login($params) {
        //组装参数
        $data = [
            'openId' => session('openid'),
            'userName' => $params['username'],
            'userPwd' => $params['password'],
            'loginDevice' => $_SERVER['HTTP_USER_AGENT'],
            'loginSource' => 'WAP',
            'ip' => request()->ip(),
        ];
        $result = $this->getWS('ucenter', 'users@login', $data);
        
        if($result['status'] != '2000'){
            return $this->parse_data(['code' => 5000, 'msg' => $result['msg']]);
        }
        
        session('uid', $result['result']['u_code']);
        session('username', $params['username']);
        session('openid',$result['result']['open_id']);
        if(session('partner_id')){
            if(session('order_price')){
                $jump_url = '/user/authform?order_price='.session('order_price');
            }else{
                $jump_url = '/user/authform';
            }
        }else{
            // $jump_url = session('jump_url');
//            $jump_url = '/user/authform';
            $jump_url = '/user/mechanism';
        }
        
        return $this->parse_data(['data' => ['jumpUrl' => $jump_url]]);
    }

    /*
     * 退出
     */
    public function logout() {
        session(null);
        return redirect('/payIndex');
    }

    /**
     * 登录
     */
    public function payLogin($params) {
//        $params['p_id'] = 206;
        //组装参数
        $data = [
            'openId' => session('openid'),
            'userName' => $params['username'],
            'userPwd' => $params['password'],
            'loginDevice' => $_SERVER['HTTP_USER_AGENT'],
            'loginSource' => 'WAP',
            'ip' => request()->ip(),
        ];

        $result = $this->getWS('ucenter', 'users@login', $data);
        if($result['status'] != '2000'){
            return $this->parse_data(['code' => 5000, 'msg' => $result['msg']]);
        }

        session('uid', $result['result']['u_code']);
        session('username', $params['username']);
        session('openid',$result['result']['open_id']);

        if(session('qrcode_id')){
            $qrco = "&qrcode_id=".session('qrcode_id');
        }

        if(session('p_id')){
            $jump_url = '/confirmOrder?p_id='.session('p_id').$qrco;//待付款页面
        }else{
            if(session('protocol_no')){
                $jump_url = '/authentication';//协议付认证入口
            }else{
                $jump_url = '/payIndex';//H5入口
            }
        }

        return $this->parse_data(['data' => ['jumpUrl' => $jump_url]]);
    }

    /**
     * Desc: 验证码登录
     * User: liuning
     * Date: 2018/12/19 18:38
     * Return: array
     * Update:
     */
    public function codeLogin($params) {
        //组装参数
        $data = [
            'openId' => session('openid'),
            'userName' => $params['username'],
            'loginDevice' => $_SERVER['HTTP_USER_AGENT'],
            'loginSource' => 'WAP',
            'ip' => request()->ip(),
        ];
        if (empty($params['code']) || (cookie($params['username']) <> $params['code'])) {
            return $this->parse_data(['code' => 5000, 'msg' => '短信验证码不正确，请重新输入']);
        }

        $result = $this->getWS('ucenter', 'users@codeLogin', $data);
        if($result['status'] != '2000'){
            return $this->parse_data(['code' => 5000, 'msg' => $result['msg']]);
        }

        session('uid', $result['result']['u_code']);
        session('username', $params['username']);
        session('openid',$result['result']['open_id']);

        if(session('qrcode_id')){
            $qrco = "&qrcode_id=".session('qrcode_id');
        }

        if(session('p_id')){
            $jump_url = '/confirmOrder?p_id='.session('p_id').$qrco;//待付款页面
        }else{
            $jump_url = '/payIndex';//H5入口
        }

        return $this->parse_data(['data' => ['jumpUrl' => $jump_url]]);
    }
}
